<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\ChangePasswordRequest;

class UserController extends Controller
{
    //
	public function avatar()
	{
		return view('users.avatar');
	}

	public function changeAvatar(Request $request)
	{
		$file = $request->file('img');
		$filename = md5(time().user()->id) . '.' . $file->getClientOriginalExtension();
		$file->move(public_path('images/avatars'), $filename);

		user()->avatar = '/images/avatars/' . $filename;
		//Storage::disk('qiniu')->writeStream($filename,fopen($file->getRealPath(),'r'));
		//user()->avatar = 'http://'.config('filesystems.disks.qiniu.domain').'/'.$filename;

		user()->save();
		return ['url' => user()->avatar];
	}

	/**
	 * @return mixed
	 */
	public function password()
	{
		return view('users.password');
	}


	/**
	 * @param ChangePasswordRequest $request
	 * @return mixed
	 */
	public function passwordUpdate(ChangePasswordRequest $request)
	{
		// 用 \Hash::check 和用 password_verify() 的结果是一样的

		if (\Hash::check($request->get('old_password'), user()->password)) {
			user()->password = bcrypt($request->get('password'));
			user()->save();
			\Flash::success('密码修改成功');
			return back();
		}
		\Flash::error('密码修改失败');
		return back();
	}
}
